import json
from pyecharts.charts import Map
from pyecharts.options import *
#读取数据文件
f = open("E:\\py代码\\疫情.txt", "r", encoding="utf-8")
c_data = f.read()  #得到全部数据

#关闭文件
f.close()

#取到各省的数据
#将字符串json转换为python字典
data_dict = json.loads(c_data) #基础数据字典
#从字典取出省份数据
province_data_list = data_dict["areaTree"][0]["children"]
#组装每个省份和确诊人数为元组，并将各个省份的数据封装到列表内
data_list = [] #绘图用到的列表
for province_data in province_data_list:
    if province_data["name"] in ["北京", "上海", "天津", "重庆"]:
        province_name = province_data["name"] + "市"
    elif province_data["name"] in ["内蒙古", "西藏"]:
        province_name = province_data["name"] + "自治区"
    elif province_data["name"] == "新疆":
        province_name = province_data["name"] + "维吾尔自治区"
    elif province_data["name"] == "广西":
        province_name = province_data["name"] + "壮族自治区"
    elif province_data["name"] == "宁夏":
        province_name = province_data["name"] + "回族自治区"
    else:
        province_name = province_data["name"] + "省"

    print(f"处理后名称: {province_name}")
    province_confirm = province_data["total"]["confirm"] #确诊人数
    data_list.append((province_name, province_confirm))
    print(data_list)
print(province_name)
#创建地图对象
map = Map()
#添加数据
map.add("中国疫情",data_list,"china")
#设置全局配置
map.set_global_opts(
    title_opts = TitleOpts(title = "全国疫情地图"),
    visualmap_opts = VisualMapOpts(
        is_show=True,
        is_piecewise=True,
        pieces=[
            {"min": 1, "max": 9, "label": "1-9", "color": "#FF0000"},  # 红色
            {"min": 100, "max": 999, "label": "100-999", "color": "#FF6600"},  # 橙色
            {"min": 1000, "max": 4999, "label": "1000-4999", "color": "#FFFF00"},  # 黄色
            {"min": 5000, "max": 9999, "label": "5000-9999", "color": "#00FF00"},  # 绿色
            {"min": 10000, "max": 99999, "label": "10000-99999", "color": "#0000FF"},  # 蓝色
            {"min": 100000, "label": "100000+", "color": "#990033"},  # 深红色
        ]
    )
)

#绘图
map.render("map++.html")